package com.baosight.risk.web.riaa;

import com.baosight.risk.api.riaa.RiquAssessService;
import com.baosight.risk.api.riaa.RiquInquiryCompExtService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.common.annotation.Log;
import com.baosight.risk.common.enums.BusinessType;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.common.utils.ExcelUtil;
import com.baosight.risk.service.riaa.entity.RiquAssess;
import com.baosight.risk.service.riaa.entity.RiquInquiryComp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * 问卷填报扩展
 */
@Slf4j
@RestController
@RequestMapping("/riqu/inquiryExt")
@Api(value = "问卷填报扩展", description = "问卷填报扩展", produces = MediaType.APPLICATION_JSON)
public class RiquInquiryCompExtController extends BaseController {

    @Autowired
    private RiquInquiryCompExtService inquiryCompExtService;

    @Autowired
    private RiquAssessService riquAssessService;

    @GetMapping("getById")
    @ApiOperation("问卷调研部门/公司管理员下发查询")
    public AjaxResult getById(String id) {
        return success(inquiryCompExtService.inquiryCompList(id));
    }

    @PostMapping("saveInquiryExt")
    @Log(title = "问卷调研新增填报人员", businessType = BusinessType.UPDATE)
    @ApiOperation("问卷调研新增填报人员")
    public AjaxResult saveInquiry(@RequestBody List<RiquInquiryComp> compList) throws Exception {
        inquiryCompExtService.saveInquiryCompExt(compList);
        return success();
    }

    @GetMapping("deleteInquiryExt")
    @Log(title = "问卷调研删除", businessType = BusinessType.DELETE)
    @ApiOperation("问卷调研删除")
    public AjaxResult deleteDetailById(@RequestParam("ids") List<String> ids) throws Exception {
        inquiryCompExtService.deleteInquiryCompExt(ids);
        return success();
    }

    @PostMapping("selectList")
    @Log(title = "问卷评估查询", businessType = BusinessType.INSERT)
    @ApiOperation("问卷评估查询")
    public AjaxResult selectList(@RequestBody RiquAssess assess) {
        startPage();
        //return getResultPage(riquAssessService.queryList(assess));
        return getResultPage(riquAssessService.queryListGroupByRiskNo(assess));
    }
    @GetMapping("saveRiquAssess")
    @Log(title = "问卷评估新增", businessType = BusinessType.INSERT)
    @ApiOperation("问卷评估新增")
    public AjaxResult saveRiquAssess(@RequestParam("quId") String quId) throws Exception {
        return success(riquAssessService.insertRiquAssess(quId));
    }

    @PostMapping("updateRiquAssess")
    @Log(title = "问卷评估修改", businessType = BusinessType.UPDATE)
    @ApiOperation("问卷评估修改")
    public AjaxResult updateRiquAssess(@RequestBody List<RiquAssess> assessList){
        if (CollectionUtils.isEmpty(assessList)) {
            return error("数据不能为空！");
        } else {
            riquAssessService.updateRiquAssess(assessList);
            return success();
        }
    }

    @PostMapping("createRisk")
    @Log(title = "生成风险自评", businessType = BusinessType.UPDATE)
    @ApiOperation("生成风险自评")
    public AjaxResult createRisk(@RequestBody List<RiquAssess> assessList) throws Exception {
        if (CollectionUtils.isEmpty(assessList)) {
            return error("数据不能为空！");
        } else {
            riquAssessService.createRisk(assessList);
            return success();
        }
    }

    @PostMapping("createRiskImport")
    @Log(title = "生成重点风险", businessType = BusinessType.UPDATE)
    @ApiOperation("生成重点风险")
    public AjaxResult createRiskImport(@RequestBody List<RiquAssess> assessList) throws Exception {
        if (CollectionUtils.isEmpty(assessList)) {
            return error("数据不能为空！");
        } else {
            riquAssessService.createRiskImport(assessList);
            return success();
        }
    }

    @PostMapping("export")
    @Log(title = "问卷评估导出", businessType = BusinessType.INSERT)
    @ApiOperation("问卷评估导出")
    public AjaxResult exportExcel(@RequestBody RiquAssess assess) {
        log.info("导出问卷分析参数：" + assess.toString());
        ExcelUtil<RiquAssess> excel = new ExcelUtil<>(RiquAssess.class);
        return excel.exportExcel(riquAssessService.queryList(assess), "问卷评估",null);
    }

}
